<?php

namespace App\Http\Controllers\Admin;

use App\Models\Config;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ConfigController extends Controller
{

    public function site(Request $request)
    {
        $id = $request->get('id', 1);
        $config = Config::find($id);
        if ($config['site_open']) {
            $config['site_open'] = getAliOssImage($config['site_open']);
        }
        if ($config['privacy']) {
            $config['privacy'] = replaceOssImgSrc($config['privacy']);
        }
        return showMsg('操作成功', 200, $config);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function siteupdate(Request $request)
    {
        $data = $request->only(['id', 'site_mobile', 'site_address', 'site_email', 'site_title', 'site_icp', 'site_logo', 'site_open', 'privacy', 'agreement', 'site_copyright']);
        //存在更新
        if ($data['site_open']) {
            $data['site_open'] = delAliOssImage($data['site_open']);
        }
        if ($data['privacy']) {
            $data['privacy'] = replaceImgSrc($data['privacy']);
        }
        if ($data['id']) {
            $article = Config::find($data['id']);
            if ($article->update($data)) {
                return showMsg('更新成功', 200);
            }
        } else {
            $article = Config::create($data);
            if ($article) {
                return showMsg('更新成功', 200);
            }
        }
    }


    public function app()
    {
        $choose[0] = "setting";
        $choose[1] = "app";
        $config = Config::find(1);
        return view('admin.config.app', compact('config', 'choose'));
    }

    public function appupdate(Request $request)
    {
        $data = $request->only(['id', 'app_version', 'app_urls', 'app_desc']);
        //存在更新
        if ($data['id']) {
            $article = Config::find($data['id']);
            if ($article->update($data)) {
                return redirect(route('admin.app'))->with(['status' => '更新成功']);
            }
        } else {
            $article = Config::create($data);
            if ($article) {
                return redirect(route('admin.app'))->with(['status' => '更新成功']);
            }
        }
    }

}
